package yxy.game.pm2.active.module.guanyurecruit;

import org.joda.time.DateTime;
import yxy.apple.util.redisson.RedisException;
import yxy.cherry.data.bean.actrecruitcharge;
import yxy.game.pm2.active.module.ActOpenHandle;
import yxy.game.pm2.active.module.DrawStatus;
import yxy.game.pm2.active.module.OpenType;
import yxy.game.pm2.active.module.actdata.ActDataCenter;
import yxy.game.pm2.active.module.lockorder.LockOrderInfo;
import yxy.game.pm2.bean.db.dao.MySQLCacheDAO;
import yxy.game.pm2.bean.module.PlayerModule;
import yxy.game.pm2.module.conquest.ConquestModule;

import java.util.HashMap;
import java.util.Map;

public class ActiveModule_关羽招募 extends PlayerModule {
	// bean --------------------------------------------
	public static final int acttype = 118;
	
	private HashMap<Integer, GYRecrit> gymap = new HashMap<Integer, GYRecrit>();

	public HashMap<Integer, GYRecrit> getGymap() {
		return gymap;
	}
	public void setGymap(HashMap<Integer, GYRecrit> gymap) {
		this.gymap = gymap;
	}
	
	// func --------------------------------------------
	
	private static ActiveModule_关羽招募 create(String playerCode) {
		ActiveModule_关羽招募 am = new ActiveModule_关羽招募();
		am.setPlayerCode(playerCode);
		am.set();
		return am;
	}
	
	private void update() {

		ConquestModule cm = ConquestModule.get(getPlayerCode());
		if(cm == null) {
			return;
		}
		
		Map<String, actrecruitcharge> datamap = ActDataCenter.getAllData(actrecruitcharge.class, getPlayerCode());
		for(actrecruitcharge data : datamap.values()) {
			if(getGymap().containsKey(data.getId())) {
				continue;
			}
			
			if(cm.getMapId() >= data.getCondition()) {
				GYRecrit gy = new GYRecrit();
				gy.setId(data.getId());
				gy.setGoodsid(data.getPurchasever());
				
				DateTime now = DateTime.now();
				DateTime end = now.plusSeconds(data.getType());
				gy.setEnd(end);
				
				getGymap().put(data.getId(), gy);
				set();
			}
		}
		
		//检查锁单
		DateTime now = DateTime.now();
		boolean save = false;
		for(GYRecrit gy : getGymap().values()) {
			if(gy.getLock() != null) {
				if(now.getMillis() - gy.getLock().getMillis() > LockOrderInfo.lockTime()) {
					gy.setLock(null);
					save = true;
				}
			}
		}
		
		if(save) {
			set();
		}
	}
	
//	public static final int canget = 1;
//	public static final int hadget = 2;
//	public static final int cannotget = 3;
	
	public int buy(int goodsid) {
		for(GYRecrit gy : getGymap().values()) {
			if(gy.getGoodsid() == goodsid) {
				if(gy.getBuy() != null) {
					return DrawStatus.hadget.getStatus();
				}
				
				if(gy.getLock() == null) {
					return DrawStatus.canget.getStatus();
				}
			}
		}
		return DrawStatus.cannotget.getStatus();
	}
	
	public void lockorder(int goodsid) {
		for(GYRecrit gy : getGymap().values()) {
			if(gy.getGoodsid() == goodsid) {
				gy.setLock(DateTime.now());
				set();
				return;
			}
		}
	}
	
	public void buyunlockorder(int goodsid) {
		for(GYRecrit gy : getGymap().values()) {
			if(gy.getGoodsid() == goodsid) {
				gy.setLock(null);
				gy.setBuy(DateTime.now());
				set();
				return;
			}
		}
	}
	
	// redis -------------------------------------------- 

	static private String key(String playerCode) {
		return String.format("PLAYER:%s:ACTIVE:%S", playerCode, acttype);
	}

	public void set() {
		try {
			setv(key(getPlayerCode()), this);
			check();
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 获取 ActiveModule_关羽招募
	 * 
	 * @param playerCode
	 * @return
	 */
	static public ActiveModule_关羽招募 get(String playerCode, Boolean isNewPlayer) {
		try {
			ActiveModule_关羽招募 module = getv(key(playerCode));
			if (module == null) {

				//新用户不需要去查数据库
				if(!isNewPlayer){
					module = MySQLCacheDAO.getV(key(playerCode), ActiveModule_关羽招募.class);
					if(module != null){module.set();}//同步到redis
				}

				if (module == null) {
					module = create(playerCode);
				}
			}
			if (module == null) {
				return null;
			}

			module.update();
			module.check();

			return module;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/** 查看是否有可领取奖励 控制红点 */
	public void check() {
		for(GYRecrit gy : getGymap().values()) {
			if(gy.getEnd().isAfterNow()) {
				new ActOpenHandle().updateActOpen(getPlayerCode(), acttype, OpenType.开启);
				return;
			}
		}
			
		new ActOpenHandle().updateActOpen(getPlayerCode(), acttype, OpenType.关闭);
	}

	//存储到数据库的方法
	public static void saveToDB(String playerCode) {
		try {
			String key = key(playerCode);
			if (touchBucket(key).isExists()) {
				ActiveModule_关羽招募 activeModule_关羽招募 = getv(key(playerCode));
				MySQLCacheDAO.setV(key, activeModule_关羽招募);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	//从redis移走数据的操作
	public static void removeAllFromRedis(String playerCode) {
		try {
			touchBucket(key(playerCode)).delete();
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}

	// lock ---------------------------------------------
	@Override
	public String lockKey() {
		// TODO Auto-generated method stub
		return null;
	}



}
